package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
public class DishController {

    //依赖注入
    @Autowired
    private DishService dishService;

    //分页+条件查询
    @GetMapping("/dish/page")
    //1.就接受前段传来的参数
    private ResultInfo findByPage(
            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            String name) {
        // 2. 调用service
        Page<Dish> page = dishService.findByPage(pageNum,pageSize,name);
        // 3.返回结果
        return ResultInfo.success(page);
    }

    // 新增菜品
    @PostMapping("/dish")
    // 1. 接收参数
    public ResultInfo save(@RequestBody Dish dish) {
        // 2.调用service
        dishService.save(dish);
        // 3.返回结果
        return ResultInfo.success();
    }
    //回显菜品
    @GetMapping("/dish/{id}")
    // 1.接收前端传来的参数
    public ResultInfo findById(@PathVariable Long id) {
        // 2.调用service
        Dish dish = dishService.findById(id);
        //返回结果
        return ResultInfo.success(dish);
    }

    // 修改菜品
    @PutMapping("/dish")
    public ResultInfo updateById(@RequestBody Dish dish) {
        //调用service
        dishService.updateById(dish);
        // 返回结果
        return ResultInfo.success();
    }

    //删除菜品
    @DeleteMapping("/dish")
    //1.接收参数
    public ResultInfo deleteBatchIds(@RequestParam List<Long> ids) {
        //2.调用service
        dishService.deleteBatchIds(ids);
        //3.返回结果
        return ResultInfo.success();
    }

    //停售菜品
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        //调用service
        dishService.updateStatus(status,ids);
        //返回结果
        return ResultInfo.success();
    }

    //  根据分类查询菜品列表
    @GetMapping("/dish/list")
    // 1.接收参数
    public ResultInfo findListByCategoryId(Long categoryId,String name) {
        // 2.调用service
        List<Dish> dishList = dishService.findListByCategoryId(categoryId,name);
        // 返回结果
        return ResultInfo.success(dishList);
    }




}
